<?php

// Fatal Error Handler
// Registering shutdown function
register_shutdown_function('fatal_handler');
function fatal_handler()
{
    // Getting last error
    global $CONFIG;
    $errno = error_get_last();
    if ($errno['type'] === E_ERROR) {
        $body = "<table><thead bgcolor='#c8c8c8'><th>Item</th><th>Description</th></thead><tbody>";
        $body .= "<tr valign='top'><td><b>Error</b></td><td><pre>{$errno['message']}</pre></td></tr>";
        $body .= "<tr valign='top'><td><b>Errno</b></td><td><pre>{$errno['type']}</pre></td></tr>";
        $body .= "<tr valign='top'><td><b>File</b></td><td>{$errno['file']}</td></tr>";
        $body .= "<tr valign='top'><td><b>Line</b></td><td>{$errno['line']}</td></tr>";
        $body .= '</tbody></table>';
        $body .= '<br />$_POST<br/>';
        $body .= print_r($_POST, true);
        $body .= '<br />$_SERVER<br/>';
        $body .= print_r($_SERVER, true);
        if ($CONFIG->attributes()->debug == 'true' || $_SERVER['REMOTE_ADDR'] == '150.101.230.130') {
            die($body);
        }
        $subject = 'Fatal error - Admin Area';
        $to = 'readysteadygokids@gmail.com';
        $from = (string) $CONFIG->company->name;
        $from = empty($from) ? 'website' : $from;
        $fromEmail = 'noreply@'.str_replace('www.', '', $HTTP_HOST);
        /* To send HTML mail, you can set the Content-type header. */
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .= "X-Priority: 3\r\n";
        $headers .= 'X-Mailer: PHP'.phpversion()."\r\n";
        /* additional headers */
        $headers .= 'Reply-To: '.$from.' <'.$fromEmail.">\r\n";
        $headers .= 'Return-Path: '.$from.' <'.$fromEmail.">\r\n";
        $headers .= 'From: '.$from.' <'.$fromEmail.">\r\n";
        $headers .= "Bcc: readysteadygokids@bccgmail.com\r\n";
        try {
            $filename = $_SERVER['DOCUMENT_ROOT'].'fatalerror'.date('Y-m-d-H').'.txt';
            if (!file_exists($filename)) {
                file_put_contents($filename, $body);
                mail($to, $subject, $body, $headers, "-f $fromEmail");
            }
        } catch (Exception $e) {
        }

        $_SESSION['error'] = 'We had trouble saving your entry. Please review your entry and try again. If this continues please contact us.';
        header('location: /503.html');
        die('@ 503 Service Temporarily Unavailable');
    }
}

set_include_path($_SERVER['DOCUMENT_ROOT']);
ini_set('display_errors', 0);
error_reporting(0);

$_CONF_FILE = "/admin/config/config.xml.php";
$xml = file_get_contents($_SERVER['DOCUMENT_ROOT'] . $_CONF_FILE);
$GLOBALS['CONFIG'] = simplexml_load_string($xml);
if ($GLOBALS['CONFIG'] !== false) {
    $debug = (string)$CONFIG->attributes()->debug;
    if ($debug == 'true') {
        ini_set('display_errors', 1);
        ini_set('error_reporting', E_ALL ^ E_NOTICE ^ E_WARNING ^ E_STRICT);
    } //ENABLE ERRORS IF DEBUG ON
} else {
    ini_set('display_errors', 1);
    echo "configuration file not loaded";
    die();
}

include_once 'admin/includes/1card1/OpenApiClient.php';
include_once 'admin/includes/functions/admin-functions-general.php';
include_once 'admin/includes/classes/listing-class.php';
include_once 'admin/includes/classes/record-class.php';
include_once 'admin/includes/classes/product-class.php';
include_once 'admin/includes/classes/ecardeapi-class.php';
include_once 'admin/includes/classes/messageboard-class.php';

include_once 'database/pdo-db-manager.php';
include_once 'database/table-class.php';
include_once 'database/utilities.php';
include_once 'smarty/Smarty.class.php';

include_once 'database/user-class.php';
include_once 'database/member-class.php';
include_once 'database/cart-class.php';
include_once 'database/enrolment-class.php';
include_once 'database/listing-class.php';
include_once 'database/product-class.php';
include_once 'database/classterm-class.php';
include_once 'database/CommBank-class.php';
include_once 'database/shipping-class.php';

$GLOBALS['EDITED'] = 'edited';
$GLOBALS['DELETED'] = 'deleted';
$GLOBALS['WARNING'] = 'warning';
$GLOBALS['ERROR'] = 'error';
$GLOBALS['HTTP_HOST'] = (!empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '');

ini_set('session.gc_maxlifetime', 7200);
session_set_cookie_params(7200);
session_start();

if (!empty($CONFIG->ecarde->openid) && !empty($CONFIG->ecarde->secret)) {
    $Cardclient = new OpenApiClient($CONFIG->ecarde->openid, $CONFIG->ecarde->secret);
} else {
    $Cardclient = new OpenApiClient();
}

$_REQUEST = clean($_REQUEST);
$_POST = clean($_POST);
$_GET = clean($_GET);
$DBobject = new DBmanager();

$SMARTY = new Smarty();
$SMARTY->template_dir = $_SERVER['DOCUMENT_ROOT'].'/admin'.$CONFIG->smartytemplate_config->templates;                    // name of directory for templates
$SMARTY->compile_dir = $_SERVER['DOCUMENT_ROOT'].'/admin'.$CONFIG->smartytemplate_config->templates_c;     // name of directory for compiled templates
$SMARTY->plugins_dir = array($_SERVER['DOCUMENT_ROOT'].''.$CONFIG->smartytemplate_config->plugins, $_SERVER['DOCUMENT_ROOT'].'/smarty/plugins');  // plugin directories
$SMARTY->cache_dir = $_SERVER['DOCUMENT_ROOT'].''.$CONFIG->smartytemplate_config->cache;                    // name of directory for cache
if ($debug == 'true' || $_REQUEST['debug'] == 'true') {
    $SMARTY->debugging = true;
    $SMARTY->force_compile = true;
    $SMARTY->caching = false;
} else {
    $SMARTY->debugging = false;
    $SMARTY->force_compile = false;
    $SMARTY->caching = false;
}

$curSym = (string) $CONFIG->currency->symbol;
$GLOBALS['CURRENCY_SYMBOL'] = empty($curSym) ? '$' : $curSym;
$SMARTY->assign('CURRENCY_SYMBOL', $GLOBALS['CURRENCY_SYMBOL']);
$curCode = (string) $CONFIG->currency->code;
$GLOBALS['CURRENCY_CODE'] = empty($curCode) ? 'AUD' : $curCode;
$SMARTY->assign('CURRENCY_CODE', $GLOBALS['CURRENCY_CODE']);
$taxCode = (string) $CONFIG->tax->code;
$GLOBALS['TAX_CODE'] = empty($taxCode) ? 'GST' : $taxCode;
$SMARTY->assign('TAX_CODE', $GLOBALS['TAX_CODE']);
$taxRate = floatval($CONFIG->tax->rate);
$GLOBALS['TAX_RATE'] = empty($taxRate) ? 0 : $taxRate;
$SMARTY->assign('TAX_RATE', $GLOBALS['TAX_RATE']);

$GLOBALS['PHONE'] = (string) $CONFIG->company->phone;
$SMARTY->assign('PHONE', $GLOBALS['PHONE']);

$staging = (string) $CONFIG->attributes()->staging;
if ($staging === 'true') {
    $SMARTY->assign('staging', true);
} else {
    $SMARTY->assign('staging', false);
}
